/*******************************************************************************
 * AppDelegate.m
 * ComicsBlog
 *
 * Adrien Bougouin <adrien.bougouin@gmail.com>
 */

#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)            application:(UIApplication *) application
  didFinishLaunchingWithOptions:(NSDictionary *)  options {
  CGRect screenBounds = [[UIScreen mainScreen] bounds];

  _window = [[UIWindow alloc] initWithFrame:screenBounds];
  _menuViewController = [[UIViewController alloc] init];

  [self createMenu];
  [_window setRootViewController:_menuViewController];
  [_window makeKeyAndVisible];

  return YES;
}

- (void) applicationWillEnterForeground:(UIApplication *) application {
  // TODO reload page if displayed
}

- (void) applicationWillTerminate:(UIApplication *) application {
  [_window              release];
  [_menuViewController  release];
}

//==============================================================================
// Menu creation and management
//==============================================================================

- (void) createMenu {
  // add a new autorelease pool to the stack
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  NSBundle *mainBundle = [NSBundle mainBundle];
  NSString *resources = [[mainBundle bundlePath] stringByAppendingString:@"/"];
  CGRect applicationFrame = [[UIScreen mainScreen] applicationFrame];
  UIView *menu = [[UIView alloc] initWithFrame:applicationFrame];
  CGRect bounds = [menu bounds];
  int screenWidth = CGRectGetWidth(bounds);
  int screenHeight = CGRectGetHeight(bounds);
  int menuWidth = screenWidth;
  int menuHeight = screenHeight - (HEADER_HEIGHT + 5);
  CGRect headerRect = CGRectMake((screenWidth / 2) - (HEADER_WIDTH / 2),
                                 0,
                                 HEADER_WIDTH,
                                 HEADER_HEIGHT);
  CGRect actusVORect = CGRectMake((menuWidth / 4) - (MENU_BUTTON_HEIGHT / 2),
                                  HEADER_HEIGHT + (menuHeight / 6) - (MENU_BUTTON_HEIGHT / 2),
                                  MENU_BUTTON_WIDTH,
                                  MENU_BUTTON_HEIGHT);
  CGRect actusVFRect = CGRectMake(((menuWidth / 4) * 3) - (MENU_BUTTON_HEIGHT / 2),
                                  HEADER_HEIGHT + (menuHeight / 6) - (MENU_BUTTON_HEIGHT / 2),
                                  MENU_BUTTON_WIDTH,
                                  MENU_BUTTON_HEIGHT);
  CGRect rdvRect = CGRectMake((menuWidth / 4) - (MENU_BUTTON_HEIGHT / 2),
                              HEADER_HEIGHT + ((menuHeight / 6) * 3) - (MENU_BUTTON_HEIGHT / 2),
                              MENU_BUTTON_WIDTH,
                              MENU_BUTTON_HEIGHT);
  CGRect ecransRect = CGRectMake(((menuWidth / 4) * 3) - (MENU_BUTTON_HEIGHT / 2),
                                HEADER_HEIGHT + ((menuHeight / 6) * 3) - (MENU_BUTTON_HEIGHT / 2),
                                MENU_BUTTON_WIDTH,
                                MENU_BUTTON_HEIGHT);
  CGRect goodiesRect = CGRectMake((menuWidth / 4) - (MENU_BUTTON_HEIGHT / 2),
                                  HEADER_HEIGHT + ((menuHeight / 6) * 5) - (MENU_BUTTON_HEIGHT / 2),
                                  MENU_BUTTON_WIDTH,
                                  MENU_BUTTON_HEIGHT);
  CGRect trashbagRect = CGRectMake(((menuWidth / 4) * 3) - (MENU_BUTTON_HEIGHT / 2),
                                   HEADER_HEIGHT + ((menuHeight / 6) * 5) - (MENU_BUTTON_HEIGHT / 2),
                                   MENU_BUTTON_WIDTH,
                                   MENU_BUTTON_HEIGHT);
  NSString *backgroundImagePath = [resources stringByAppendingString:BACKGROUND_IMAGE];
  UIImage *backgroundImage = [[UIImage alloc] initWithContentsOfFile:backgroundImagePath];
  UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:bounds];
  NSString *headerImagePath = [resources stringByAppendingString:HEADER_IMAGE];
  UIImage *headerImage = [[UIImage alloc] initWithContentsOfFile:headerImagePath];
  UIImageView *headerView = [[UIImageView alloc] initWithFrame:headerRect];
  NSString *actusVOSelImagePath = [resources stringByAppendingString:BTN_ACTUS_VO_SEL_IMAGE];
  UIImage *actusVOSelImage = [[UIImage alloc] initWithContentsOfFile:actusVOSelImagePath];
  NSString *actusVOUSelImagePath = [resources stringByAppendingString:BTN_ACTUS_VO_USEL_IMAGE];
  UIImage *actusVOUSelImage = [[UIImage alloc] initWithContentsOfFile:actusVOUSelImagePath];
  UIButton *actusVOButton = [[UIButton alloc] initWithFrame:actusVORect];
  NSString *actusVFSelImagePath = [resources stringByAppendingString:BTN_ACTUS_VF_SEL_IMAGE];
  UIImage *actusVFSelImage = [[UIImage alloc] initWithContentsOfFile:actusVFSelImagePath];
  NSString *actusVFUSelImagePath = [resources stringByAppendingString:BTN_ACTUS_VF_USEL_IMAGE];
  UIImage *actusVFUSelImage = [[UIImage alloc] initWithContentsOfFile:actusVFUSelImagePath];
  UIButton *actusVFButton = [[UIButton alloc] initWithFrame:actusVFRect];
  NSString *rdvSelImagePath = [resources stringByAppendingString:BTN_RDV_SEL_IMAGE];
  UIImage *rdvSelImage = [[UIImage alloc] initWithContentsOfFile:rdvSelImagePath];
  NSString *rdvUSelImagePath = [resources stringByAppendingString:BTN_RDV_USEL_IMAGE];
  UIImage *rdvUSelImage = [[UIImage alloc] initWithContentsOfFile:rdvUSelImagePath];
  UIButton *rdvButton = [[UIButton alloc] initWithFrame:rdvRect];
  NSString *ecransSelImagePath = [resources stringByAppendingString:BTN_ECRANS_SEL_IMAGE];
  UIImage *ecransSelImage = [[UIImage alloc] initWithContentsOfFile:ecransSelImagePath];
  NSString *ecransUSelImagePath = [resources stringByAppendingString:BTN_ECRANS_USEL_IMAGE];
  UIImage *ecransUSelImage = [[UIImage alloc] initWithContentsOfFile:ecransUSelImagePath];
  UIButton *ecransButton = [[UIButton alloc] initWithFrame:ecransRect];
  NSString *goodiesSelImagePath = [resources stringByAppendingString:BTN_GOODIES_SEL_IMAGE];
  UIImage *goodiesSelImage = [[UIImage alloc] initWithContentsOfFile:goodiesSelImagePath];
  NSString *goodiesUSelImagePath = [resources stringByAppendingString:BTN_GOODIES_USEL_IMAGE];
  UIImage *goodiesUSelImage = [[UIImage alloc] initWithContentsOfFile:goodiesUSelImagePath];
  UIButton *goodiesButton = [[UIButton alloc] initWithFrame:goodiesRect];
  NSString *trashbagSelImagePath = [resources stringByAppendingString:BTN_TRASHBAG_SEL_IMAGE];
  UIImage *trashbagSelImage = [[UIImage alloc] initWithContentsOfFile:trashbagSelImagePath];
  NSString *trashbagUSelImagePath = [resources stringByAppendingString:BTN_TRASHBAG_USEL_IMAGE];
  UIImage *trashbagUSelImage = [[UIImage alloc] initWithContentsOfFile:trashbagUSelImagePath];
  UIButton *trashbagButton = [[UIButton alloc] initWithFrame:trashbagRect];
  UIColor *cbColor = [[UIColor alloc] initWithRed:(47.0 / 255.0)
                                            green:(70.0 / 255.0)
                                             blue:(85.0 / 255.0)
                                            alpha:1.0];

  // configuration
  [menu setBackgroundColor:cbColor];
  [backgroundView setImage:backgroundImage];
  [headerView setImage:headerImage];
  [actusVOButton setImage:actusVOSelImage forState:UIControlStateHighlighted];
  [actusVOButton setImage:actusVOUSelImage forState:UIControlStateNormal];
  [actusVFButton setImage:actusVFSelImage forState:UIControlStateHighlighted];
  [actusVFButton setImage:actusVFUSelImage forState:UIControlStateNormal];
  [rdvButton setImage:rdvSelImage forState:UIControlStateHighlighted];
  [rdvButton setImage:rdvUSelImage forState:UIControlStateNormal];
  [ecransButton setImage:ecransSelImage forState:UIControlStateHighlighted];
  [ecransButton setImage:ecransUSelImage forState:UIControlStateNormal];
  [goodiesButton setImage:goodiesSelImage forState:UIControlStateHighlighted];
  [goodiesButton setImage:goodiesUSelImage forState:UIControlStateNormal];
  [trashbagButton setImage:trashbagSelImage forState:UIControlStateHighlighted];
  [trashbagButton setImage:trashbagUSelImage forState:UIControlStateNormal];

  // menu selection handling
  [actusVOButton addTarget:self
                    action:BTN_ACTUS_VO_SEL
          forControlEvents:UIControlEventTouchUpInside];
  [actusVFButton addTarget:self
                    action:BTN_ACTUS_VF_SEL
          forControlEvents:UIControlEventTouchUpInside];
  [rdvButton addTarget:self
                    action:BTN_RDV_SEL
          forControlEvents:UIControlEventTouchUpInside];
  [ecransButton addTarget:self
                    action:BTN_ECRANS_SEL
          forControlEvents:UIControlEventTouchUpInside];
  [goodiesButton addTarget:self
                    action:BTN_GOODIES_SEL
          forControlEvents:UIControlEventTouchUpInside];
  [trashbagButton addTarget:self
                    action:BTN_TRASHBAG_SEL
          forControlEvents:UIControlEventTouchUpInside];

  // add views to the window
  [menu addSubview:backgroundView];
  [menu addSubview:headerView];
  [menu addSubview:actusVOButton];
  [menu addSubview:actusVFButton];
  [menu addSubview:rdvButton];
  [menu addSubview:ecransButton];
  [menu addSubview:goodiesButton];
  [menu addSubview:trashbagButton];

  [_menuViewController setView:menu];

  // memory management
  [menu                   release];
  [backgroundImage        release];
  [backgroundView         release];
  [headerImage            release];
  [headerView             release];
  [actusVOSelImage        release];
  [actusVOUSelImage       release];
  [actusVOButton          release];
  [actusVFSelImage        release];
  [actusVFUSelImage       release];
  [actusVFButton          release];
  [rdvSelImage            release];
  [rdvUSelImage           release];
  [rdvButton              release];
  [ecransSelImage         release];
  [ecransUSelImage        release];
  [ecransButton           release];
  [goodiesSelImage        release];
  [goodiesUSelImage       release];
  [goodiesButton          release];
  [trashbagSelImage       release];
  [trashbagUSelImage      release];
  [trashbagButton         release];
  [cbColor                release];

  // delete the local autorelease pool then dealloc autoreleased objects used by
  // the method
  [pool release];
}

- (void) backToMenu {
  [_window setRootViewController:_menuViewController];
}

- (void) openPageForCategory:(NSUInteger) category {
  CBPageViewController *page = nil;
  SEL sel = @selector(backToMenu);

  page = [[CBPageViewController alloc] initWithCategory:category
                                             homeTarget:self
                                           homeSelector:sel];

  [_window setRootViewController:page];
}

- (void) openActusVOPage {
  [self openPageForCategory:CBPageActusVOCat];
}

- (void) openActusVFPage {
  [self openPageForCategory:CBPageActusVFCat];
}

- (void) openRDVPage {
  [self openPageForCategory:CBPageRDVCat];
}

- (void) openEcransPage {
  [self openPageForCategory:CBPageEcransCat];
}

- (void) openGoodiesPage {
  [self openPageForCategory:CBPageGoodiesCat];
}

- (void) openTrashbagPage {
  [self openPageForCategory:CBPageTrashbagCat];
}

@end

